package cn.school.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;

@WebFilter(filterName = "EncodingFilter",value="/*",initParams = {@WebInitParam(name = "encoding",value = "utf-8")})
public class EncodingFilter implements Filter {
    String enc;
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
       //req.setCharacterEncoding("utf-8");
        req.setCharacterEncoding(enc);
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {
        enc=config.getInitParameter("encoding");
    }

}
